<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.checkout.php 415 2014-01-18 16:23:35Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 415 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-18 16:23:35 +0000 (Sat, 18 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
require_once (DIR_FS_INC.'inc.get_shippingtime_functions.php');
require_once (DIR_FS_INC.'inc.get_clean_id.php');

class checkout {
	var $db;
	var $update_js;
	var $save_button = false;

	function __construct() {
		global $db;

		$this->db = $db;
	}

	function __destruct() {}

	function getDSG($which) {
		switch ($which) {
			case 'text' :
				if (DISPLAY_DATENSCHUTZ_ON_CHECKOUT == 'true') {
					if (GROUP_CHECK == 'true')
						$group_check = "AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";

					$shop_content_data = $this->db->db_query("SELECT
																	content_title,
																	content_heading,
																	content_text,
																	content_file
																FROM
																	".TABLE_CONTENT_MANAGER."
																WHERE
																	content_group='2' ".$group_check."
																AND
																	languages_id='".$_SESSION['languages_id']."'");

					if ($shop_content_data->fields['content_file'] != '') {
						if($shop_content_data->fields['content_file'] == 'janolaw_datenschutz.php') {
							include_once(DIR_FS_INC.'inc.janolaw.php');
							$conditions = JanolawContent('datenschutzerklaerung', 'txt');

						} 
            else {
							//$conditions = '<iframe src="'.DIR_WS_CATALOG.'media/content/'.$shop_content_data->fields['content_file'].'" width="100%" height="300"></iframe>';
              ob_start();
              include (DIR_FS_CATALOG.'media/content/'.$shop_content_data->fields['content_file']);
              $content_body = utf8_encode(ob_get_contents());
          		ob_end_clean();
          		$conditions = '<div class="div_textarea">'.$content_body.'</div>';
            }
					} else
						$conditions = '<div class="div_textarea">'.$shop_content_data->fields['content_text'].'</div>';

					return $conditions;
				}
				break;

			case 'link' :
				if (DISPLAY_DATENSCHUTZ_ON_CHECKOUT == 'true')
					return href_link('shop_content.php','coID=2');
				break;

			case 'checkbox' :
				if (DISPLAY_DATENSCHUTZ_ON_CHECKOUT == 'true')
					return '<input type="checkbox" value="dsg" name="dsg" class="radiobox" />';
				break;

			case 'stat' :
				if (DISPLAY_DATENSCHUTZ_ON_CHECKOUT == 'true')
					return 1;
				else
					return 0;
				break;
		}
	}

	function getAGB($which) {
		switch ($which) {
			case 'text' :
				if (DISPLAY_CONDITIONS_ON_CHECKOUT == 'true') {
					if (GROUP_CHECK == 'true')
						$group_check = "AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";

					$shop_content_data = $this->db->db_query("SELECT
																	content_title,
																	content_heading,
																	content_text,
																	content_file
																FROM
																	".TABLE_CONTENT_MANAGER."
																WHERE
																	content_group='3' ".$group_check."
																AND
																	languages_id='".$_SESSION['languages_id']."'");

					if($shop_content_data->fields['content_file'] != '') {
						if($shop_content_data->fields['content_file'] == 'janolaw_agb.php') {
							include_once(DIR_FS_INC.'inc.janolaw.php');
							$conditions = JanolawContent('agb', 'txt');

						} 
            else { 
							//$conditions = '<iframe src="'.DIR_WS_CATALOG.'media/content/'.$shop_content_data->fields['content_file'].'" width="100%" height="300"></iframe>';
              ob_start();
              include (DIR_FS_CATALOG.'media/content/'.$shop_content_data->fields['content_file']);
              $content_body = utf8_encode(ob_get_contents());
          		ob_end_clean();
          		$conditions = '<div class="div_textarea">'.$content_body.'</div>';
            }
					} else
						$conditions = '<div class="div_textarea">'.$shop_content_data->fields['content_text'].'</div>';

					return $conditions;
				}
				break;


			case 'link' :
				if (DISPLAY_CONDITIONS_ON_CHECKOUT == 'true')
					return href_link('shop_content.php','coID=3');
				break;

			case 'checkbox' :
				if (DISPLAY_CONDITIONS_ON_CHECKOUT == 'true')
					return '<input type="checkbox" value="conditions" name="conditions" class="radiobox" />';
				break;

			case 'stat' :
				if (DISPLAY_CONDITIONS_ON_CHECKOUT == 'true')
					return 1;
				else
					return 0;
				break;
		}
	}

	function getRevocation($which) {
		switch ($which) {
			case 'text' :
				if (DISPLAY_REVOCATION_ON_CHECKOUT == 'true') {
					if (GROUP_CHECK == 'true')
						$group_check = "AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";

					$shop_content_data = $this->db->db_query("SELECT
					                                                content_title,
					                                                content_heading,
					                                                content_text,
					                                                content_file
							                                   FROM
							                                   		".TABLE_CONTENT_MANAGER."
							                                   WHERE
							                                   		content_group='".REVOCATION_ID."' ".$group_check."
							                                   AND
							                                   		languages_id='".$_SESSION['languages_id']."'");

					if ($shop_content_data->fields['content_file'] != '') {
						if($shop_content_data->fields['content_file'] == 'janolaw_widerruf.php') {
							include_once(DIR_FS_INC.'inc.janolaw.php');
							$conditions = JanolawContent('widerrufsbelehrung', 'txt');

						} 
            else {
							//$conditions = '<iframe src="'.DIR_WS_CATALOG.'media/content/'.$shop_content_data->fields['content_file'].'" width="100%" height="300"></iframe>';
              ob_start();
              include (DIR_FS_CATALOG.'media/content/'.$shop_content_data->fields['content_file']);
              $content_body = utf8_encode(ob_get_contents());
          		ob_end_clean();
          		$conditions = '<div class="div_textarea">'.$content_body.'</div>';
            }
					} else
						$conditions = '<div class="div_textarea">'.$shop_content_data->fields['content_text'].'</div>';

					return $conditions;
				}
				break;


			case 'link' :
				if (DISPLAY_REVOCATION_ON_CHECKOUT == 'true')
					return href_link('shop_content.php','coID='.REVOCATION_ID);

				break;

			case 'checkbox' :
				if (DISPLAY_REVOCATION_ON_CHECKOUT == 'true')
					return '<input type="checkbox" value="revocation" name="revocation" class="radiobox" />';

				break;

			case 'stat' :
				if (DISPLAY_REVOCATION_ON_CHECKOUT == 'true')
					return 1;
				else
					return 2;

				break;
		}
	}

	function getFormUrl() {
		if (isset ($GLOBALS[$_SESSION['payment']]->form_action_url) && !$GLOBALS[$_SESSION['payment']]->tmpOrders)
			$form_action_url = $GLOBALS[$_SESSION['payment']]->form_action_url;
		else
			$form_action_url = href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');

		$form_action_url = utf8_encode($form_action_url);

		return $form_action_url;
	}

	function getIp () {
		if (SHOW_IP_LOG == 'true') {
			if ($_SERVER["HTTP_X_FORWARDED_FOR"])
				$customers_ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			else
				$customers_ip = $_SERVER["REMOTE_ADDR"];

			return $customers_ip;

		} else
			return '';
	}

	function getProductsBlock() {
		global $order, $price;
		$max_shipping_time_ranking = 0;
		$module_content = array();

		for ($i = 0, $n = sizeof($order->products); $i < $n; $i ++) {
			$products[$i] = $order->products[$i];

			$max_shipping_time_ranking = max($max_shipping_time_ranking, get_shippingtime_rank('name', $products[$i]['shipping_time']));
			$attributes_exist = ( (isset($products[$i]['attributes']) && count($products[$i]['attributes']) > 0) ? true : false);
			$options_exist = ( (isset($products[$i]['options']) && count($products[$i]['options']) > 0) ? true : false);

			$p_price = $products[$i][($attributes_exist||$options_exist ? 'price' : 'single_price')];
			$t_price = $price->format( $p_price * $products[$i]['quantity'], true );
			$p_price = $price->format( $p_price, true );
	
			$module_content[$i] = array(
				'PRODUCTS_NAME' => $products[$i]['name'],
				'PRODUCTS_ID' => $products[$i]['id'],
				'PRODUCTS_ID_CLEAN' => get_clean_id($products[$i]['id']),
				'PRODUCTS_QTY' => $products[$i]['quantity'],
				'QTY' => $products[$i]['quantity'],
				'PRODUCTS_MODEL' => $products[$i]['model'],
				'PRODUCTS_NAME_SUFFIX' => $products[$i]['name_suffix'],
				'PRODUCTS_DESCRIPTION' => $products[$i]['desc'],
				'PRODUCTS_SHIPPING_TIME' => (ACTIVATE_SHIPPING_STATUS == 'true' ? $products[$i]['shipping_time']:''),
				'PRODUCTS_TAX' => number_format($products[$i]['tax_class_id'], TAX_DECIMAL_PLACES),
				'PRODUCTS_IMAGE' => $products[$i]['image'],
				'IMAGE_ALT' => $products[$i]['name'],
				'PRODUCTS_LINK' => href_link(FILENAME_PRODUCT_INFO, product_link($products[$i]['id'], $products[$i]['name'])),
				'PRODUCTS_SINGLE_PRICE' => $price->format($products[$i]['single_price'], true),
				'PRODUCTS_PRICE' => $p_price,
				'PRODUCTS_PRICE_TOTAL' => $t_price,
				'ATTRIBUTES' => '',
				'OPTIONS' => ''
			);
								
			if ($options_exist) {
				if ( ($konfigurator = $_SESSION['cart']->use_konfigurator(true, get_prid($products[$i]['id']))) !== false ) {
					$module_content[$i]['OPTIONS'] = array(
						'DESCRIPTION' => $konfigurator->get_description($products[$i]['options'])
					);
				}
			}

			if ($attributes_exist == 1) {
				$count_attr_value = '';
				$count_attr = '';
				$ii = 0;
				$a_price = '';
				reset($products[$i]['attributes']);
				while (list ($option, $value) = each($products[$i]['attributes'])) { 
					$ii++;
					if (ATTRIBUTE_STOCK_CHECK == 'true' && STOCK_CHECK == 'true') {
						$attribute_stock_check = check_stock_attributes($products[$i][$option]['products_attributes_id'], $products[$i]['quantity']);
						if ($attribute_stock_check)
							$_SESSION['any_out_of_stock'] = 1;
					}

					if($products[$i][$option]['products_options_name'] != 'Downloads') {
						$a_s_price = $price->format($products[$i][$option]['options_values_price'], true, $products[$i]['tax_class_id']);
						$a_price = $price->format($products[$i][$option]['options_values_price'] * $products[$i]['quantity'], true, $products[$i]['tax_class_id']);
					}


					$module_content[$i]['ATTRIBUTES'][] = array(
						'ID' => $products[$i][$option]['products_attributes_id'],
						'MODEL' => get_attributes_model(get_prid($products[$i]['id']), $products[$i][$option]['products_options_values_name'],$products[$i][$option]['products_options_name']),
						'NAME' => $products[$i][$option]['products_options_name'],
						'ATTR_QTY' => ($products[$i][$option]['products_options_name'] != 'Downloads')? $products[$i]['quantity'].'x':'',
						'PRICE' => $a_price,
						'SINGLE_PRICE' => $a_s_price,
						'MODEL' => $products[$i][$option]['products_attributes_model'],
						'ATTR_ID' => get_prid($products[$i]['id']).'_'.$option.'_'.$value,
						'PREFIX' =>  ($products[$i][$option]['products_options_name'] != 'Downloads')?$products[$i][$option]['price_prefix']:'',
						'VALUE_NAME' => $products[$i][$option]['products_options_values_name'].$attribute_stock_check
					);
					$count_attr_value += $products[$i][$option]['options_values_price'];
					$count_attr = $ii;
				}
				$module_content[$i]['PRODUCTS_ATTR_COUNT'] = $count_attr+2;
			}
		}
		
		$module_smarty = new Smarty();
		$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
		$module_smarty->assign('language', $_SESSION['language']);
		$module_smarty->assign('module_content', $module_content);
		$module_smarty->caching = false;
		$module = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/order_details_checkout.html');
		return $module;
	}

	function getProducts() {
		global $order;
		global $price;
		global $smarty;
	  $max_shipping_time_ranking = 0;

		$data_products = '<div id="checkout_products_standart">
							<table class="checkout_products_table">';

		$data_products .= '		<tr>
									<th colspan="3">&nbsp;</th>
									<th>'.SINGLE_PRICE.'</th>
									<th>'.COUNT.'</th>
									<th class="ta-r">'.TOTAL_PRICE.'</th>
								</tr>';
		for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
			$html_update_qty = '';
			$pImg = '';
			$attribut_products = '';
    	$max_shipping_time_ranking = max($max_shipping_time_ranking, get_shippingtime_rank('name', $order->products[$i]['shipping_time']));
			
			if (CHECKOUT_AJAX_PRODUCTS == 'true') {
				$html_update_qty = ' <nobr><a href="javascript:void(0);" onclick="updateProducts(\''.$order->products[$i]['id'].'\',\'minus\');">[-]</a> <a href="javascript:void(0);" onclick="updateProducts(\''.$order->products[$i]['id'].'\',\'plus\');">[+]</a> <a href="javascript:void(0);" onclick="removeProduct(\''.$order->products[$i]['id'].'\',\'minus\');"> <span style="color:#CC0000">[x]</span> </a></nobr>';
			}

			if(!empty($order->products[$i]['image']))
				$pImg = '<img class="img_border" src="'.$order->products[$i]['image'].'" alt="'.$order->products[$i]['name'].'" />';
			
			if ((isset ($order->products[$i]['attributes'])) && (sizeof($order->products[$i]['attributes']) > 0)) {
				for ($j = 0, $n2 = sizeof($order->products[$i]['attributes']); $j < $n2; $j++) {
					$attribut_products .= '<div class="chk_attribute di fs85">';
						
					$attribut_products .= '	<span class="fwb">'.$order->products[$i]['attributes'][$j]['option'].':</span>
											<span id="atta_'.$i.$j.'">
												<span id="attt_'.$i.$j.'">'.$order->products[$i]['attributes'][$j]['value'].'</span>
											</span>
											<span id="attd_'.$i.$j.'" class="dn">
												'.$this->getAllPossibleAttributes($order->products[$i]['id'], $order->products[$i]['attributes'][$j]['option_id'], $order->products[$i]['attributes'][$j]['value'], $i.$j).'
											</span>';
					$attribut_products	.='</div>';
				}
			}

      $html_update_qty = '';
			$data_products .= '	<tr id="product_'.$order->products[$i]['id'].'">
									<td colspan="2">
										<span class="fwb db" style="margin-bottom:5px; font-size:13px;">'.$order->products[$i]['name'].'</span>'.
										
										(ACTIVATE_SHIPPING_STATUS=='true'?'<span class="fs85"><!-- em>'.SHIPPING_TIME.$order->products[$i]['shipping_time'].'</em --></span><div>':'').
										$attribut_products.'
									</div></td>
									<td class="w15p">'.$html_update_qty.'</td>
									<td class="ac">
										<span id="single_price_'.$order->products[$i]['id'].'"><nobr>' .
											$price->format($order->products[$i]['price'], true).'</nobr>
										</span>
									</td>
									<td class="ac">
										<span id="qty_'.$order->products[$i]['id'].'">'.$order->products[$i]['qty'].'</span>x
									</td>
									<td class="ta-r">
										<span id="price_'.$order->products[$i]['id'].'"><nobr>' .
											$price->format($order->products[$i]['final_price'], true).'</nobr>
										</span>
									</td>
								</tr>';

			if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
				if (sizeof($order->info['tax_groups']) > 1){
					require_once (DIR_FS_INC.'inc.display_tax_value.php');
					$data_products .= '<td class="va-t ta-r p5"><span id="tax_'.$order->products[$i]['id'].'">'.display_tax_value($order->products[$i]['tax']).'</span>%</td>';
				}
			}
			$data_products .= '</tr>';
		}
		$data_products .= '</table></div>';

    if (is_object($smarty))
      $smarty->assign('SHIPPING_TIME', get_shippingtime_name('ranking', $max_shipping_time_ranking));
    else      	
  		$data_products .= '<div>'.SHIPPING_TIME.get_shippingtime_name('ranking', $max_shipping_time_ranking).'</div>';

		return $data_products;
	}

	function getAllPossibleAttributes($pid, $oid, $selected, $aid) {
		global $price;

		$dropdown = '<SELECT id="att_'.$aid.'_'.$oid.'" name="att_'.$pid.$oid.'" size="1" onchange="updateAttributes(this.id, \''.$aid.'\')">';
		$query1 = $this->db->db_query("SELECT * FROM ".TABLE_PRODUCTS_ATTRIBUTES." WHERE products_id = '".$pid."' AND options_id = '".$oid."'");

		if($query1->_numOfRows <= 1) {
			// nur ein Attribut
			$query2 = $this->db->db_query("SELECT
												*
											FROM
												".TABLE_PRODUCTS_OPTIONS_VALUES."
											WHERE
												language_id = '".$_SESSION['languages_id']."'
											AND
												products_options_values_id = '".$query1->fields['options_values_id']."'");

			return $query2->fields['products_options_values_name'];

		} else {
			// mehr als ein Attribut
			while (!$query1->EOF) {
				$query2 = $this->db->db_query("SELECT
													*
												FROM
													".TABLE_PRODUCTS_OPTIONS_VALUES."
												WHERE
													language_id = '".$_SESSION['languages_id']."'
												AND
													products_options_values_id = '".$query1->fields['options_values_id']."'");
				// Check if selected
				$html_selected = '';
				if (html_entity_decode($query2->fields['products_options_values_name']) == html_entity_decode($selected))
					$html_selected = ' selected';

				$dropdown .= '<option value="'.$pid.'|'.$oid.'|'.$query2->fields['products_options_values_id'].'"'.$html_selected.'>
								'.$query2->fields['products_options_values_name'].($query1->fields['options_values_price']>0?' '.$query1->fields['price_prefix'].' '.$price->format($query1->fields['options_values_price'], true):'').'
								</option>';
				$query1->MoveNext();
			}
		}
		$dropdown .= '</select>';
		return $dropdown;
	}

	function getStat($option) {
		$query = $this->db->db_query("SELECT
											configuration_value
										FROM
											".TABLE_CONFIGURATION."
										WHERE
											configuration_key = 'CHECKOUT_SHOW_".strtoupper($option)."'");
		if($query->fields['configuration_value'] != 'true')
			return ' dn';
	}
	
	function getStatSymbol($option) {
		$query = $this->db->db_query("SELECT
											configuration_value
										FROM
											".TABLE_CONFIGURATION."
										WHERE
											configuration_key = 'CHECKOUT_SHOW_".strtoupper($option)."'");
		if($query->fields['configuration_value'] != 'true')
			return '+';
		else
			return '-';
	}

	function getAddresses($which) {
		if($which == 'shipping')
			$sess = 'sendto';
		elseif($which == 'payment')
			$sess = 'billto';

		$dropdown = '<SELECT id="select_shipping_address" name="select_shipping_address" size="1" onchange="updateAddressBySelect(\''.$which.'\', this.value);">';
		$addresses = $this->db->db_query("SELECT
												address_book_id,
												entry_firstname AS firstname,
												entry_lastname AS lastname,
												entry_company AS company,
												entry_street_address AS street_address,
												entry_suburb AS suburb,
												entry_city AS city,
												entry_postcode AS postcode,
												entry_state AS state,
												entry_zone_id AS zone_id,
												entry_country_id AS country_id
											FROM
												".TABLE_ADDRESS_BOOK."
											WHERE customers_id = '".$_SESSION['customer_id']."'");

		while(!$addresses->EOF) {
			$format_id = get_address_format_id($addresses->fields['country_id']);
			$selected = '';

			if ($addresses->fields['address_book_id'] == $_SESSION[$sess])
				$selected = ' selected';

			$short_address = address_format($format_id, $addresses->fields, true, ' ', ', ');
      if (strlen($short_address) < 25)
        $strpos = false;
      else
        $strpos = strpos($short_address, ',', 25);

			if ($strpos != false)
				$short_address = substr($short_address, 0, $strpos+1).' ...';

			$dropdown .= '<option value="'.$addresses->fields['address_book_id'].'"'.$selected.'>'.$short_address.'</option>';

			$addresses->MoveNext();
		}
		$dropdown .= '</select>';

		return $dropdown;

	}

	function getEditAddressButton($which) {
		return '<a href="javascript:void(0);" onclick="editAddress(\''.$which.'\')">'.image_button('button_checkout_edit.gif',IMAGE_BUTTON_EDIT).'</a>';
	}

	function getSaveAddressButton($which) {
		return '<a href="javascript:void(0);" onclick="updateAddress(\''.$which.'\')">'.image_button('button_checkout_save.gif',IMAGE_BUTTON_SAVE).'</a>';
	}

	function getBackButton($which) {
		return '<a href="javascript:void(0);" onclick="cancelAddress(\''.$which.'\')">'.image_button('button_back.gif',IMAGE_BUTTON_BACK).'</a>';
	}

	function getSaveModuleButton($which) {
		if($which == 'payment')
			return '<a href="javascript:void(0);" class="db" onclick="updatePaymentModule()">'.image_button('button_checkout_save.gif',IMAGE_BUTTON_SAVE,'id="button_save_payment"').'</a>';
		elseif($which == 'shipping')
			return '<a href="javascript:void(0);" class="db" onclick="updateShippingModule()">'.image_button('button_checkout_save.gif',IMAGE_BUTTON_SAVE,'id="button_save_shipping"').'</a>';
	}

	function newAddressPossible() {
		$addresses = $this->db->db_query("SELECT
												address_book_id,
												entry_firstname AS firstname,
												entry_lastname AS lastname,
												entry_company AS company,
												entry_street_address AS street_address,
												entry_suburb AS suburb,
												entry_city AS city,
												entry_postcode AS postcode,
												entry_state AS state,
												entry_zone_id AS zone_id,
												entry_country_id AS country_id
											FROM
												".TABLE_ADDRESS_BOOK."
											WHERE
												customers_id = '".$_SESSION['customer_id']."'");


		if($addresses->_numOfRows >= MAX_ADDRESS_BOOK_ENTRIES)
			return 0;
		else
			return 1;
	}

	function isVirtual() {
		global $order;
		$virtual = 0;
		if ($order->content_type == 'virtual' || ($order->content_type == 'virtual_weight') || ($_SESSION['cart']->count_contents_virtual() == 0)) {
			unset($_SESSION['shipping']);
			unset($_SESSION['payment']);
			$virtual = 1;
		}
		return $virtual;
	}

	function getStates($country_id) {
		if (ACCOUNT_STATE == 'true') {
			$zone_id = 0;
			$check = $this->db->db_query("SELECT COUNT(*) AS total FROM ".TABLE_ZONES." WHERE zone_country_id = '".$country_id."'");

			if($check->_numOfRows) {
				$dropdown = '<SELECT size="1">';
				$dropdown .= '</select>';
			}

		} else
			return false;
	}

	function convertToVars($query_string) {
		$vars = array();
		if(strpos($query_string, '&') === false) {
			$string = str_replace('"', '', stripslashes($query_string));
			$string = str_replace('{', '', $string);
			$string = str_replace('}', '', $string);

			$exploded = explode(',', $string);

			for($i=0; sizeof($exploded) > $i; $i++) {
				$trennen = explode(':', $exploded[$i]);
				if($trennen[0] != '' && $trennen[1] !='')
					$vars[$trennen[0]] = $trennen[1];
			}

		} else {
			if (!is_array($query_string)) {
				$arr_vars = explode('&',$query_string);
				foreach ($arr_vars as $value) {
					$arr_vars2 = explode('=',$value);
					$vars[$arr_vars2[0]] = urldecode($arr_vars2[1]);
				}
			} else {
				reset($query_string);
				while (list($key, $value) = each ($query_string))
					$vars[$key] = urldecode($value);
			}
		}
		return $vars;
	}

	function isFreeShipping($order, $price) {
		if(defined('MODULE_SHIPPING_FREEAMOUNT_STATUS') && MODULE_SHIPPING_FREEAMOUNT_STATUS == 'True') {			
			if($price->RemoveCurr($_SESSION['cart']->show_total()) > MODULE_SHIPPING_FREEAMOUNT_AMOUNT)
				return true;
			else
				return false;
		} else
			return false;
	}

	function getTaxID($id) {
		$cart_products = $_SESSION['cart']->get_products();
		for ($i=0; $i<count($cart_products); $i++) {
			if ($cart_products[$i]['id'] == $id) {
				$tax_class_id = $cart_products[$i]['tax_class_id'];
				break;
			}
		}
		return $tax_class_id;
	}

	function getShippingBlock($price) {
		global $shipping_modules;
		global $order;
		global $total_weight;
		global $total_count;
		global $shipping_compatible;

		if(!is_object($shipping_modules)) {
			require (DIR_WS_CLASSES.'class.shipping.php');
			$shipping_modules = new shipping();
			$free_shipping = $this->isFreeShipping($order, $price);
			if($free_shipping)
				include(DIR_WS_LANGUAGES.$_SESSION['language'].'/modules/order_total/ot_shipping.php');
		}

		$quotes = $shipping_modules->quote();
		$module_smarty = new Smarty();
		if (count_shipping_modules() > 0) {
			$showtax = $_SESSION['customers_status']['customers_status_show_price_tax'];

			$radio_buttons = 0;
			$something_checked = false;

			for ($i = 0, $n = sizeof($quotes); $i < $n; $i ++) {
				if(!isset ($quotes[$i]['error'])) {
					for ($j = 0, $n2 = sizeof($quotes[$i]['methods']); $j < $n2; $j ++) {
						$quotes[$i]['methods'][$j]['radio_buttons'] = $radio_buttons;
						$checked = (($quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'] == $_SESSION['shipping']['id'] || $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'] == $_SESSION['shipping']) ? true : false);
						if (($checked == true) || ($n == 1 && $n2 == 1)) {
							$quotes[$i]['methods'][$j]['checked'] = 1;
							$something_checked = true;
						}

						if (($n > 1) || ($n2 > 1)) {
							if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0)
								$quotes[$i]['tax'] = '';
							if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0)
								$quotes[$i]['tax'] = 0;

							$quotes[$i]['methods'][$j]['price'] = $price->format(add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax']), true, 0, true);
							$quotes[$i]['methods'][$j]['radio_field'] = draw_radio_field('shipping', $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'], $checked, 'onclick="updateShippingModule(); if(this.checked) { shippingShowOrHide(\'smodule'.$radio_buttons.'\'); this.checked=true; return true; }"').draw_hidden_field($quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'].'_num', $radio_buttons+1);
							$quotes[$i]['methods'][$j]['value_id'] = $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'];

						} else {
							if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0) {
								$quotes[$i]['tax'] = 0;
							}

							if ((isset ($quotes[$i]['methods'][$j]['title'])) && (isset ($quotes[$i]['methods'][$j]['cost']))) {
								$_SESSION['shipping'] = array('id' => $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'], 
															'title' => (($this->isFreeShipping($order, $price) == true) ? $quotes[$i]['methods'][$j]['title'] : $quotes[$i]['module'].' '.$quotes[$i]['methods'][$j]['title']), 
															'cost' => $quotes[$i]['methods'][$j]['cost']);
							}
							$quotes[$i]['methods'][$j]['value_id'] = $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'];
							$quotes[$i]['methods'][$j]['price'] = $price->format(add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax']), true, 0, true).draw_hidden_field('shipping', $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id']).draw_hidden_field($quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'].'_num', $radio_buttons+1);
						}
						$radio_buttons ++;
					}
				}
			}

			if($something_checked == false && !empty($_SESSION['shipping']['id'])) {
				unset($_SESSION['shipping']);
				$shipping_compatible = false;
			}

			$module_smarty->assign('module_content', $quotes);
			$module_smarty->assign('module_choose', CHECKOUT_SHIPPING_CHOOSE);
			$module_smarty->caching = false;
			if (CHECKOUT_VIEW == 'Dropdown')
				$shipping_block = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/checkout_shipping_block_dropdown.html');
			else
				$shipping_block = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/checkout_shipping_block.html');

			return $shipping_block;
		}
	}

	function getPaymentBlock($price, $payment_modules) {
		global $payment_compatible;
		$module_smarty = new Smarty();

		$something_checked = false;
		$selection = $payment_modules->selection();
		$radio_buttons = 0;
		
		for ($c = 0, $n = sizeof($selection); $c < $n; $c++) {
			if(!empty($selection[$c]['fields']))
				$this->save_button = true;
		}
		
		if(!$this->save_button)
			$this->update_js = 'updatePaymentModule(); ';

		for ($i = 0, $n = sizeof($selection); $i < $n; $i++) {
			$selection[$i]['radio_buttons'] = $radio_buttons;
			if (($selection[$i]['id'] == $_SESSION['payment']) || ($n == 1)) {
				$selection[$i]['checked'] = 1;
				$something_checked = true;
			}
			if (empty($selection[$i]['module_cost']) && isset($GLOBALS[$selection[$i]['id']]->cost))
				$selection[$i]['module_cost'] = CHECKOUT_PAYMENT_DUE;

			if (sizeof($selection) > 1) {
				$selection[$i]['selection'] = draw_radio_field('payment', $selection[$i]['id'], ($selection[$i]['id'] == $_SESSION['payment']), 'class="payment_selection" id="'.$selection[$i]['id'].'" onclick="'.$this->update_js.'openPayment(\''.$selection[$i]['id'].'\');if(this.checked){paymentShowOrHide(\'pmodule_'.$selection[$i]['id'].'\'); this.checked=true;return true;}"').draw_hidden_field($selection[$i]['id'].'_num', $i+1);
			} else {
				$only_one = 1;
				$selection[$i]['selection'] = draw_hidden_field('payment', $selection[$i]['id'], '').draw_hidden_field($selection[$i]['id'].'_num', $i+1);

				if (!is_array($selection[$i]['fields']) && !isset($selection[$i]['fields'])) {
					if ($_SESSION['payment'] != 'no_payment') 
						$_SESSION['payment'] = $selection[$i]['id'];
				} else {
					unset($_SESSION['payment']);
					$something_checked = false;
					$selection[$i]['checked'] = 0;
				}
			}
			$selection[$i]['value_id'] = $selection[$i]['id'];

			if (isset($selection[$i]['error'])) {

			} else
				$radio_buttons++;
		}

		if($something_checked == false && !empty($_SESSION['payment'])) {
			unset($_SESSION['payment']);
			$payment_compatible = false;
		}

		$module_smarty->assign('module_content', $selection);
		$module_smarty->assign('module_choose', CHECKOUT_PAYMENT_CHOOSE);

		$module_smarty->assign('xajax', 1);
		$module_smarty->assign('only_one', $only_one);
		$module_smarty->caching = false;
		if (CHECKOUT_VIEW == 'Dropdown')
			$payment_block = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/checkout_payment_block_dropdown.html');
		else
			$payment_block = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/checkout_payment_block.html');

		return $payment_block;
	}

	function getTotalBlock($order, $order_total_modules, $price) {
		if(MODULE_ORDER_TOTAL_INSTALLED) {
			$order_total_modules->values_from_cart = true;	
			$total_block = $order_total_modules->output();
		}
		return $total_block;
	}
}